package gui.product;

import java.util.Date;

import gui.common.*;
import hit.core.Barcode;
import hit.core.HomeInventoryTracker;
import hit.core.Product;
import hit.core.ProductFacade;
import hit.core.Size;
import hit.core.Size.Unit;
import hit.dao.DataAccessObject;
import hit.dao.FactoryReference;
import hit.util.SizeConverter;

/**
 * Controller class for the edit product view.
 */
public class EditProductController extends Controller 
										implements IEditProductController {
	
	ProductFacade facade;
	
	Product toEdit;
	
	String barcode;
	String description;
	Size size;
	int shelfLife;
	int threeMonth;
	
	/**
	 * Constructor.
	 * 
	 * @param view Reference to the edit product view
	 * @param target Product being edited
	 */
	public EditProductController(IView view, ProductData target) {
		super(view);
		
		facade = HomeInventoryTracker.Instance().GetProductFacade();
		
		toEdit = (Product)target.getTag();
		barcode = toEdit.GetBarcode().GetValue();
		description = toEdit.GetDescription();
		size = toEdit.GetSize();
		shelfLife = toEdit.GetShelfLife();
		threeMonth = toEdit.GetThreeMonth();

		construct();
	}

	//
	// Controller overrides
	//
	
	/**
	 * Returns a reference to the view for this controller.
	 * 
	 * {@pre None}
	 * 
	 * {@post Returns a reference to the view for this controller.}
	 */
	@Override
	protected IEditProductView getView() {
		return (IEditProductView)super.getView();
	}

	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	@Override
	protected void enableComponents() {
		
		getView().enableSizeValue(size.GetType() != Unit.COUNT);
		
		boolean result = (description != null &&
							!description.equals("") &&
							Size.IsValid(size) &&
							(size.GetType() != Unit.COUNT ||
								size.GetAmount() == 1) &&
							shelfLife >= 0);
		result = (result && threeMonth >= 0);
		
		getView().enableOK(result);
	}

	/**
	 * Loads data into the controller's view.
	 * 
	 *  {@pre None}
	 *  
	 *  {@post The controller has loaded data into its view}
	 */
	@Override
	protected void loadValues() {
		
		getView().enableBarcode(false);
		
		getView().setBarcode(barcode);
		getView().setDescription(description);
		getView().setShelfLife(Integer.toString(shelfLife));
		getView().setSupply(Integer.toString(threeMonth));
		getView().setSizeValue(Double.toString(size.GetAmount()));
		getView().setSizeUnit(SizeConverter.ConvertModelUnitsToController(size.GetType()));
	}

	//
	// IEditProductController overrides
	//

	/**
	 * This method is called when any of the fields in the
	 * edit product view is changed by the user.
	 */
	@Override
	public void valuesChanged() {
		
		description = getView().getDescription();
		
		SizeUnits sizeUnit = getView().getSizeUnit();
		
		try {			
			if (sizeUnit == SizeUnits.Count)
			{
				getView().setSizeValue("1");
				getView().enableSizeValue(false);
			}
			else
			{
				getView().enableSizeValue(true);
			}
		
			double sizeAmount = Double.parseDouble(getView().getSizeValue());
			size = new Size(sizeAmount, SizeConverter.ConvertControllerUnitsToModel(sizeUnit));
			
			shelfLife = Integer.parseInt(getView().getShelfLife());
			threeMonth = Integer.parseInt(getView().getSupply());
			
			Product newProduct = new Product();
			newProduct.SetCreation(toEdit.GetCreation());
			newProduct.SetBarcode(toEdit.GetBarcode());
			newProduct.SetDescription(description);
			newProduct.SetShelfLife(shelfLife);
			newProduct.SetThreeMonth(threeMonth);
			newProduct.SetSize(size);
			
			getView().enableOK(facade.CanEditProduct(toEdit, newProduct));
		}
		catch (Exception e) {
			description = "";
			size = null;
			shelfLife = -1;
			threeMonth = -1;
			getView().enableOK(false);
		}
	}
	
	/**
	 * This method is called when the user clicks the "OK"
	 * button in the edit product view.
	 */
	@Override
	public void editProduct() {
		
		try {
			Product newProduct = new Product();
			newProduct.SetCreation(toEdit.GetCreation());
			newProduct.SetBarcode(toEdit.GetBarcode());
			newProduct.SetDescription(description);
			newProduct.SetShelfLife(shelfLife);
			newProduct.SetThreeMonth(threeMonth);
			newProduct.SetSize(size);
			
			facade.EditProduct(toEdit, newProduct);
			
			DataAccessObject dao = FactoryReference.GetFactory().CreateDataAccessObject();
			dao.StartTransaction();
			
			dao.GetProductDAO().Update(toEdit);
			dao.EndTransaction(true);
			dao.CloseConnection();
		}
		catch (Exception e) {
			getView().displayErrorMessage("Failed to Edit Product.");
		}
	}

}

